package com.zmj.framework.response;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

/**
 * 统一响应类
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class Result<T> {
    /**
     * 200成功
     */
    private int code;

    /**
     * 错误说明
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public Result(int code, String msg) {
        this.code = code;
        this.message = msg;
    }

    private static <T> Result generate(ResponseCode response, T data) {
        return Result.<T>builder().code(response.getCode()).message(response.getMsg()).data(data).build();
    }

    private static <T> Result generate(int code, String message, T data) {
        return Result.<T>builder().code(code).message(message).data(data).build();
    }

    public static <T> Result success(T data) {
        return generate(ResponseCode.SUCCESS, data);
    }

    public static <T> Result success() {
        return generate(ResponseCode.SUCCESS, null);
    }

    public static <T> Result fail() {
        return generate(ResponseCode.FAIL, null);
    }

    public static <T> Result fail(ResponseCode response) {
        return generate(response, null);
    }

    public static <T> Result fail(String msg) {
        return generate(ResponseCode.FAIL.getCode(), msg, null);
    }

    public static <T> Result fail(int code,String msg) {
        return generate(code, msg, null);
    }

    public boolean isOk() {
        if (this.code == ResponseCode.SUCCESS.getCode()) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return "ResultBody{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
